using System;
using System.Diagnostics;
using System.IO;
using System.Collections.Generic;
using org.tmatesoft.svn.core;
using org.tmatesoft.svn.core.wc;

namespace Subsync.Core.Subversion
{
    /// <summary>
	/// Author: Hoang Duc
	/// </summary>
    public class SVNMkdir : CommandBase
    {
        /// <summary>
        /// We have our variables declared here.
        /// Access modifiers as you can observe, is public. Why?
        /// Because we trust our programmers.
        /// 
        /// Try to be verbose with the variable names.
        /// Also, try to include *sane defaults*. Take peg_revision for eg.
        /// </summary>
        private SVNURL[] urls;
        private string commitMessage;
        private SVNProperties revisionProperties;
        private bool makeParents = true;

        /// <summary>
        /// Sane default constructor.
        /// </summary>
        /// <param name="urls">
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="commitMessage">
        /// A <see cref="System.String"/>
        /// </param>
        public SVNMkdir(string[] urls, string commitMessage)
        {
            Debug.WriteLine("SVN Mkdir minimal Constructor with sane defaults");

            this.urls = new SVNURL[urls.Length];
            for (int i = 0; i < urls.Length; i++)
                this.urls[i] = SVNURL.parseURIEncoded(urls[i]);
            this.commitMessage = commitMessage;
        }

        /// <summary>
        /// Full constructor for Mkdir.
        /// </summary>
        /// <param name="urls">
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="commitMessage">
        /// A <see cref="System.String"/>
        /// </param>
        /// <param name="revisionProperties">
        /// A <see cref="SVNProperties"/>
        /// </param>
        /// <param name="makeParents">
        /// A <see cref="System.Boolean"/>
        /// </param>
        public SVNMkdir(string[] urls,
                        string commitMessage,
                        SVNProperties revisionProperties,
                        bool makeParents)
        {
            Debug.WriteLine("SVN Mkdir normal Constructor");

            this.urls = new SVNURL[urls.Length];
            for (int i = 0; i < urls.Length; i++)
                this.urls[i] = SVNURL.parseURIEncoded(urls[i]);
            this.commitMessage = commitMessage;
            this.revisionProperties = revisionProperties;
            this.makeParents = makeParents;
        }

        /// <summary>
        /// Execute SVN Mkdir
        /// </summary>
        public override void Execute()
        {
            Debug.WriteLine("SVN Mkdir Execution");

            //library setup
            Tools.SetupLibrary();

            //load up the clients
            SVNClientManager cm = SVNClientManager.newInstance();
            SVNCommitClient cc = cm.getCommitClient();

            try
            {
                org.tmatesoft.svn.core.auth.BasicAuthenticationManager bam =
                    new org.tmatesoft.svn.core.auth.BasicAuthenticationManager("smalllittlecroc", "hh2Ey2QM7xW9");
                cc = new SVNCommitClient(bam, cm.getOptions());
                cc.doMkDir(this.urls, this.commitMessage, this.revisionProperties, this.makeParents);                
            }
            catch (SVNException e)
            {
                //we convert this exception to a non SVNKit exception.
                //we want to minimize interfacing with SVNkit outside of this namespace (abstraction)
                throw new SVNExecuteException(e.getMessage());
            }
        }

        public bool checkPath(string inputPath)
        {
            // Checks if path is for repos access or WC access
            // Returns true for WC access
            Debug.WriteLine("SVN propget method to check if input path is repos access");
            List<string> repos_access = new List<string>();
            // List of Repos access URLs
            repos_access.Add("file:///"); // Local repos
            repos_access.Add("http://");
            repos_access.Add("https://");
            repos_access.Add("svn://");
            repos_access.Add("svn+ssh://");

            foreach (string accessprefix in repos_access)
            {
                if (inputPath.StartsWith(accessprefix))
                    return false;
            }
            return true;
        }
    }
}
